package com.lanhe.notice.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.utils.StringUtils;
import com.lanhe.notice.domain.LhNotice;
import com.lanhe.notice.domain.LhNoticeContent;
import com.lanhe.notice.mapper.LhNoticeMapper;
import com.lanhe.notice.service.ILhNoticeContentService;
import com.lanhe.notice.service.ILhNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公告表(LhNotice)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-30 14:00:35
 */
@Service
public class LhNoticeServiceImpl extends ServiceImpl<LhNoticeMapper, LhNotice> implements ILhNoticeService {

    @Autowired
    private LhNoticeMapper lhNoticeMapper;

    @Autowired
    private ILhNoticeContentService noticeContentService;

    @Override
    public List<LhNotice> selectNoticeList(LhNotice notice) {

        LambdaQueryWrapper<LhNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(notice.getTitle()), LhNotice::getTitle, notice.getTitle());
        queryWrapper.ge(ObjectUtil.isNotNull(notice.getBeginTime()), LhNotice::getCreateTime, notice.getBeginTime());
        queryWrapper.le(ObjectUtil.isNotNull(notice.getEndTime()), LhNotice::getCreateTime, notice.getEndTime());
        queryWrapper.eq(LhNotice::getDelFlag, 0);
        queryWrapper.orderByDesc(LhNotice::getNoticeId);
        return lhNoticeMapper.selectList(queryWrapper);
    }

    @Override
    public LhNotice selectNoticeById(Long noticeId) {
        LambdaQueryWrapper<LhNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LhNotice::getDelFlag, 0)
                .eq(LhNotice::getNoticeId, noticeId);
        LhNotice lhNotice = lhNoticeMapper.selectOne(queryWrapper);
        LhNoticeContent noticeContent = noticeContentService.selectNoticeContentByNoticeId(noticeId);
        if (ObjectUtil.isNotNull(noticeContent)){
            lhNotice.setContent(noticeContent.getContent());
        }
        return lhNotice;
    }

    @Override
    public Integer insertNotice(LhNotice notice) {
        int insertNum = lhNoticeMapper.insert(notice);
        noticeContentService.insertNoticeContent(notice.getNoticeId(),notice.getContent());
        return insertNum;
    }

    @Override
    public int updateNotice(LhNotice notice) {
        LambdaQueryWrapper<LhNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LhNotice::getDelFlag, 0)
                .eq(LhNotice::getNoticeId, notice.getNoticeId());
        int updateNum = lhNoticeMapper.update(notice, queryWrapper);
        noticeContentService.updateNoticeContent(notice.getNoticeId(),notice.getContent());
        return updateNum;
    }

    @Override
    public int deleteNoticeByIds(Long noticeId) {
        LambdaUpdateWrapper<LhNotice> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(LhNotice::getDelFlag, Constants.GeneralIntType.TWO+"")
                .in(LhNotice::getNoticeId,noticeId);
        return lhNoticeMapper.update(null,updateWrapper);
    }

    /**
     * 获取最新的一条，首页用
     *
     * @return com.lanhe.notice.domain.LhNotice
     * @author zhanggl
     * @date 2024/1/2
     */
    @Override
    public LhNotice lastOne() {
        LambdaQueryWrapper<LhNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LhNotice::getDelFlag, 0);
        queryWrapper.orderByDesc(LhNotice::getNoticeId);
        queryWrapper.last("limit 1");
        return lhNoticeMapper.selectOne(queryWrapper);
    }
}

